//package com.lxw.shardingjdbc.interceptor;
//
//import com.baomidou.mybatisplus.annotation.TableId;
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.binding.MapperMethod;
//import org.apache.ibatis.cache.CacheKey;
//import org.apache.ibatis.executor.Executor;
//import org.apache.ibatis.executor.statement.StatementHandler;
//import org.apache.ibatis.mapping.BoundSql;
//import org.apache.ibatis.mapping.MappedStatement;
//import org.apache.ibatis.mapping.SqlCommandType;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.plugin.Intercepts;
//import org.apache.ibatis.plugin.Invocation;
//import org.apache.ibatis.plugin.Signature;
//import org.apache.ibatis.reflection.DefaultReflectorFactory;
//import org.apache.ibatis.reflection.MetaObject;
//import org.apache.ibatis.reflection.ReflectorFactory;
//import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
//import org.apache.ibatis.reflection.factory.ObjectFactory;
//import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
//import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
//import org.apache.ibatis.session.ResultHandler;
//import org.apache.ibatis.session.RowBounds;
//import org.springframework.beans.BeanUtils;
//
//import java.lang.reflect.Field;
//import java.lang.reflect.Method;
//import java.util.*;
//
///**
// * @describe: 查询拦截器
// * 查询条件加密使用方式：使用 @Param("decrypt")注解的自定义类型
// * 返回结果解密使用方式： ①在自定义的DO上加上注解 CryptAnnotation  ②在需要加解密的字段属性上加上CryptAnnotation
// * @author: ***
// * @date: 2021/3/30 17:51
// */
//@Slf4j
//@Intercepts({
//        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
//                RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
//        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
//                RowBounds.class, ResultHandler.class})
//})
//public class SqlInterceptor implements Interceptor {
//    /**
//     * 查询参数名称，ParamMap的key值
//     */
//    private static final String DECRYPT = "decrypt";
//    private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory();
//    private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();
//    private static final ReflectorFactory REFLECTOR_FACTORY = new DefaultReflectorFactory();
//
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//
//        // 获取sql信息
//        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
//        BoundSql boundSql = statementHandler.getBoundSql();
//        String sql = boundSql.getSql();
//        System.out.println("原sql为: " + sql);
//
//        // 获取元数据
//        MetaObject metaResultSetHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, REFLECTOR_FACTORY);
//        MappedStatement mappedStatement = (MappedStatement) metaResultSetHandler.getValue("delegate.mappedStatement");
//
//        // 获取调用方法
//        String id = mappedStatement.getId();
//        String className = id.substring(0, id.lastIndexOf("."));
//        String methodName = id.substring(id.lastIndexOf(".") + 1);
//        System.out.println("调用方法为: " + id);
//        String newSql = sql;
//        // 修改sql
//        if(sql.contains("where")) {
//            newSql = String.format("select * from (%s) `range` where `range`.permission in (%s)", sql, null);
//        } else {
//            newSql = String.format("select * from (%s) `range` where `range`.permission in (%s)", sql, null);
//        }
//
//        System.out.println("修改后的sql为: " + newSql);
//        // 反射修改handler中的sql以执行
//        Class boundClass = boundSql.getClass();
//        Field field = boundClass.getDeclaredField("sql");
//        field.setAccessible(true);
//        field.set(boundSql, newSql);
//
//        return invocation.proceed();
//    }
//}
